package com.xiao.community.service.impl;

import com.xiao.community.entity.GoodsEntity;
import com.xiao.community.entity.PintuanEntity;
import com.xiao.community.feign.UserFeign;
import com.xiao.community.service.GoodsService;
import com.xiao.community.service.PintuanService;
import com.xiao.community.utils.SnowFlake;
import com.xiao.group.common.Vo.UsersEntity;
import com.xiao.group.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiao.group.common.utils.PageUtils;
import com.xiao.group.common.utils.Query;

import com.xiao.community.dao.PintuanOrderDao;
import com.xiao.community.entity.PintuanOrderEntity;
import com.xiao.community.service.PintuanOrderService;


@Service("pintuanOrderService")
public class PintuanOrderServiceImpl extends ServiceImpl<PintuanOrderDao, PintuanOrderEntity> implements PintuanOrderService {

    @Autowired
    PintuanOrderDao pintuanOrderDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PintuanOrderEntity> page = this.page(
                new Query<PintuanOrderEntity>().getPage(params),
                new QueryWrapper<PintuanOrderEntity>()
        );

        return new PageUtils(page);
    }

    @Autowired
    UserFeign userFeign;

    @Autowired
    GoodsService goodsService;

    @Autowired
    PintuanService pintuanService;
    public void panduanpintuan(Integer pintuanid){
        PintuanEntity byId = pintuanService.getById(pintuanid);
        if(byId.getPintuanKucun()==0){
            byId.setPintuanStatus(0);
            pintuanService.updateById(byId);
        }

    }
    @Override
    public Integer canyupintuan(String uuid, Integer pintuanid, Integer userid) {
        PintuanOrderEntity orderEntity = baseMapper.selectOne(new QueryWrapper<PintuanOrderEntity>().eq("uuid", uuid).eq("kairuannum",1));
        PintuanOrderEntity pintuanOrderEntity = new PintuanOrderEntity();
        GoodsEntity byId = goodsService.getById(pintuanid);
        UsersEntity info = userFeign.info(userid);
        PintuanEntity pintuanEntity = pintuanService.getById(pintuanid);
        pintuanEntity.setGoodsKucun(pintuanEntity.getPintuanKucun()-2);
        pintuanService.updateById(pintuanEntity);
        //用户名
        pintuanOrderEntity.setAdminName(info.getUsername());
        //用户id
        pintuanOrderEntity.setUserid(info.getId()); //用户id
        //商品名称
        pintuanOrderEntity.setGoodsName(byId.getGoodsName());
        //商品id
        pintuanOrderEntity.setGoodsId(byId.getGoodsId());
        // 拼团 id
        pintuanOrderEntity.setPintuanid(pintuanid);
        //创建时间
        pintuanOrderEntity.setCreatetime(new Date());
        //订单 状态
        pintuanOrderEntity.setOrderStatus(0);
        //开团唯一标识符
        pintuanOrderEntity.setUuid(uuid.toString());
        //用户标识符
        pintuanOrderEntity.setKairuannum(0);
        //订单编号
        pintuanOrderEntity.setOrderId(orderEntity.getOrderId());
        int insert = baseMapper.insert(pintuanOrderEntity);
        panduanpintuan(orderEntity.getPintuanid());
        return pintuanOrderEntity.getGoodsId();
    }

    @Override
    public Integer kaituan(Integer userid, Integer pintuanid) {
        PintuanOrderEntity pintuanOrderEntity = new PintuanOrderEntity();
        GoodsEntity byId = goodsService.getById(pintuanid);
        UsersEntity info = userFeign.info(userid);
        UUID uuid = UUID.randomUUID();

        //用户名
        pintuanOrderEntity.setAdminName(info.getUsername());
        //用户id
        pintuanOrderEntity.setUserid(info.getId()); //用户id
        //商品名称
        pintuanOrderEntity.setGoodsName(byId.getGoodsName());
        //商品id
        pintuanOrderEntity.setGoodsId(byId.getGoodsId());
        // 拼团 id
        pintuanOrderEntity.setPintuanid(pintuanid);
        //创建时间
        pintuanOrderEntity.setCreatetime(new Date());
        //订单 状态
        pintuanOrderEntity.setOrderStatus(0);
        //开团唯一标识符
        pintuanOrderEntity.setUuid(uuid.toString());
        //用户标识符
        pintuanOrderEntity.setKairuannum(1);
        //订单编号
        Long ids = SnowFlake.ids();
        pintuanOrderEntity.setOrderId(ids.toString());
        int insert = baseMapper.insert(pintuanOrderEntity);
        return pintuanOrderEntity.getGoodsId();
    }


    @Override
    public R topintuanlist(Integer goodsid) {
        List<PintuanOrderEntity> pintuanId = pintuanOrderDao.selectList(new QueryWrapper<PintuanOrderEntity>().eq("pintuanid", goodsid));
        return R.ok().put("data",pintuanId);
    }

}